package get

import (
	"database/sql"
	"fmt"

	"github.com/gin-gonic/gin"
)

func checkErrenfp(err error) {
	if err != nil {
		panic(err)
	}
}

func Getmbtienfp(r *gin.Engine) {
	var db *sql.DB
	var err error
	psqlInfo := fmt.Sprintf("host=%s port=%d user=%s "+
		"password=%s dbname=%s sslmode=disable",
		host, port, user, password, dbname)
	db, err = sql.Open("postgres", psqlInfo)
	checkErrenfp(err)

	var mid int
	var mname string
	var mal string
	var feature1, feature2, feature3, feature4, feature5, feature6, feature7 string
	var behavior1, behavior2, behavior3 string
	var trend1, trend2, trend3, trend4, trend5, trend6, trend7 string
	var defect1, defect2, defect3, defect4 string
	var suggest1, suggest2, suggest3, suggest4, suggest5, suggest6 string

	r.GET("/enfp", func(c *gin.Context) {
		rows, err := db.Query("SELECT * FROM public.mbti where mid=8")
		checkErrenfp(err)

		for rows.Next() {

			err = rows.Scan(&mid, &mname, &mal,
				&feature1, &feature2, &feature3, &feature4, &feature5, &feature6, &feature7,
				&behavior1, &behavior2, &behavior3,
				&trend1, &trend2, &trend3, &trend4, &trend5, &trend6, &trend7,
				&defect1, &defect2, &defect3, &defect4,
				&suggest1, &suggest2, &suggest3, &suggest4, &suggest5, &suggest6)
			checkErrenfp(err)

		}
		// fmt.Printf("%s\n", feature5)
		// 获取参数
		c.JSON(200, gin.H{
			"intp":     "INTP-逻辑学家", // mal + "-" + mname
			"feature1": feature1, "feature2": feature2, "feature3": feature3, "feature4": feature4, "feature5": feature5, "feature6": feature6, "feature7": feature7,
			"behavior1": behavior1, "behavior2": behavior2, "behavior3": behavior3,
			"trend1": trend1, "trend2": trend2, "trend3": trend3, "trend4": trend4, "trend5": trend5, "trend6": trend6, "trend7": trend7,
			"defect1": defect1, "defect2": defect2, "defect3": defect3, "defect4": defect4,
			"suggest1": suggest1, "suggest2": suggest2, "suggest3": suggest3, "suggest4": suggest4, "suggest5": suggest5, "suggest6": suggest6,
		})
	})
}
